// pages/article_type/article_type.js
var common = require('../../lib/common.js');
var url = require('../../config/url.js');
let interstitialAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    items: [
      {
        type: 'checkbox',
        label: '分类',
        value: 'type',
        children: [
          {
            label: '毛皮',
            value: '1',
          }, {
            label: '拉链',
            value: '2',
          }],
        groups: ['001'],
      },
      {
        type: 'radio',
        label: '排序',
        value: 'mode',
        children: [{
          label: '最新加入',
          value: '0',
          checked: true
        },
        {
          label: '热门推荐',
          value: '1',
          checked: false
        }],
        groups: ['002'],
      },
    ],

    scrollTop: 0,
    towTypeId: [],
    article: [],
    mode: 0,
    page: 1,
    loadding: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ id: options.id, title: options.name });
    wx.setNavigationBarTitle({
      title: this.data.title
    });

    this.getTwoType();
    this.getArticle();

    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({ adUnitId: 'adunit-d6ae1b4034918169' })
      interstitialAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
      interstitialAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }

    // interstitialAd.show().catch((err) => {
    //   console.error(err)
    // })
  },

  onPullDownRefresh() {
    this.setData({
      page: 1,
      loadding: true,
      article: []
    });
    this.getTwoType();
    this.getArticle();
    wx.stopPullDownRefresh();
  },


  onChange(e) {
    const { checkedItems, items } = e.detail;
    let towTypeId = [];
    let mode = 0;
    items.forEach(element => {
      if (element.value == 'type') {
        element.children.forEach(item => {
          if (item.checked)
            towTypeId.push(item.value);
        })
      } else if (element.value == 'mode') {
        element.children.forEach(item => {
          if (item.checked)
            mode = item.value;
        })
      }
    });

    this.setData({
      towTypeId,
      mode,
      page: 1,
      loadding: true,
      article: []
    });
    this.getArticle();
  },

  getTwoType() {
    let items = this.data.items;
    let children = [];
    common.post(url.getTwoType, { id: this.data.id }, (res) => {
      for (let i = 0; i < res.length; i++) {
        children.push({
          label: res[i].name,
          value: res[i].id + '',
        });
      }
      items[0].children = children
      this.setData({ items });
    });

  },

  /**
  * 获取文章列表
  */
  getArticle() {
    let params = {};
    params.page = this.data.page;
    params.oneTypeId = this.data.id;
    params.mode = this.data.mode;
    this.data.towTypeId.forEach((element, index) => {
      params['towTypeId[' + index + ']'] = element;
    })

    this.setData({ loadding: true });
    common.post(url.getArticleByType, params, (data) => {
      for (let i in data) {
        data[i].url = '/pages/aritcle_detail/index?id=' + data[i].id
        if (data[i].images == null)
          data[i].images = [];
        else {
          data[i].images = (data[i].images + '').split(',');
          data[i].images = data[i].images.map((n) => {
            return n;
          })
        }
      }
      let page = this.data.page + 1;
      if (data.length == 0) page = false;
      this.setData({ article: this.data.article.concat(data), loadding: false, page });
    });
  },

  onReachBottom() {
    let loadding = this.data.loadding;
    if (loadding === true || this.data.page == false) {
      return;
    }

    this.getArticle();
  },

  onPageScroll(e) {
    this.setData(e);
  },

})